<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>15.1.1 Unit Testing Controllers 3.2.11</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="../css/main.css" type="text/css" media="screen, print" title="Style" charset="utf-8"/>
    <link rel="stylesheet" href="../css/pdf.css" type="text/css" media="print" title="PDF" charset="utf-8"/>
    <script type="text/javascript">
function addJsClass() {
    var classes = document.body.className.split(" ");
    classes.push("js");
    document.body.className = classes.join(" ");
}
    </script>
</head>

<body class="body" onload="addJsClass();">
<div id="navigation">
    <div class="navTitle">
        
        The Grails Framework
    </div>
    <div class="navLinks">
        <ul>
            <li>
                <div id="nav-summary" onmouseover="toggleNavSummary(false)" onmouseout="toggleNavSummary(true)">
                    <a href="../../guide/index.html" class="button">Table of contents</a>

                    <div id="nav-summary-childs" style="display:none;">
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/introduction.html"><strong>1</strong><span>Introduction</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/gettingStarted.html"><strong>2</strong><span>Getting Started</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/upgrading.html"><strong>3</strong><span>Upgrading</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/conf.html"><strong>4</strong><span>Configuration</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/commandLine.html"><strong>5</strong><span>The Command Line</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/profiles.html"><strong>6</strong><span>Application Profiles</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/GORM.html"><strong>7</strong><span>Object Relational Mapping (GORM)</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/theWebLayer.html"><strong>8</strong><span>The Web Layer</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/traits.html"><strong>9</strong><span>Traits</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/webServices.html"><strong>10</strong><span>Web Services</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/async.html"><strong>11</strong><span>Asynchronous Programming</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/validation.html"><strong>12</strong><span>Validation</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/services.html"><strong>13</strong><span>The Service Layer</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/staticTypeCheckingAndCompilation.html"><strong>14</strong><span>Static Type Checking And Compilation</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/testing.html"><strong>15</strong><span>Testing</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/i18n.html"><strong>16</strong><span>Internationalization</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/security.html"><strong>17</strong><span>Security</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/plugins.html"><strong>18</strong><span>Plugins</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/spring.html"><strong>19</strong><span>Grails and Spring</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/hibernate.html"><strong>20</strong><span>Grails and Hibernate</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/scaffolding.html"><strong>21</strong><span>Scaffolding</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/deployment.html"><strong>22</strong><span>Deployment</span></a>
                        </div>
                        
                        <div class="toc-item" style="margin-left:0"><a href="../../guide/contributing.html"><strong>23</strong><span>Contributing to Grails</span></a>
                        </div>
                        
                    </div>
                </div>
            </li>
            <li class="separator selected">
                <a id="ref-button" onclick="localToggle(); return false;" href="#">Quick Reference</a>
            </li>
        </ul>
    </div>


</div>

<table id="colset" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td id="col1">
            <div id="main" class="corner-all">

                
                    <div class="toc-item prev-left"><a href="../../guide/staticTypeCheckingAndCompilation.html">&lt;&lt; <strong>14</strong><span>Static Type Checking And Compilation</span></a></div>
                

                <span id='toggle-col1' class="toggle">(<a href="#" onclick="localToggle(); return false;">Quick Reference</a>)</span>

                
                    <div class="toc-item next-right"><a href="../../guide/i18n.html"><strong>16</strong><span>Internationalization</span> >></a></div>
                


                <div class="project">
                    <h1>15.1.1 Unit Testing Controllers</h1>

                    <p><strong>Version:</strong> 3.2.11</p>
                </div>

                

                
<a name="12.1.1 Unit Testing Controllers"><!-- Legacy link --></a>
<h2 id="unitTestingControllers">15.1.1 Unit Testing Controllers</h2>

<div class='contribute-btn'>
    <button type='button' class='btn btn-default' onclick='window.location.href="https://github.com/grails/grails-doc/edit/3.2.x/src/en/guide/testing/unitTesting/unitTestingControllers.adoc"'>
        <i class='fa fa-pencil-square-o'></i> Improve this doc
    </button>
</div>


<div class="sect3">
<h4 id="_the_basics">The Basics</h4>
<div class="paragraph">
<p>You use the <code>grails.test.mixin.TestFor</code> annotation to unit test controllers. Using <code>TestFor</code> in this manner activates the <code>grails.test.mixin.web.ControllerUnitTestMixin</code> and its associated API. For example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">&quot;</span><span class="content">test something</span><span class="delimiter">&quot;</span></span>() {
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Adding the <code>TestFor</code> annotation to a controller causes a new <code>controller</code> field to be automatically created for the controller under test.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The <code>TestFor</code> annotation will also automatically annotate any public methods starting with "test" with JUnit 4&#8217;s @Test annotation. If any of your test method don&#8217;t start with "test" just add this manually
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>To test the simplest "Hello World"-style example you can do the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="comment">// Test class</span>
<span class="type">class</span> <span class="class">SimpleController</span> {
    <span class="keyword">def</span> <span class="function">hello</span>() {
        render <span class="string"><span class="delimiter">&quot;</span><span class="content">hello</span><span class="delimiter">&quot;</span></span>
    }
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">&quot;</span><span class="content">test hello</span><span class="delimiter">&quot;</span></span>() {
        <span class="key">when</span>:
        controller.hello()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">hello</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>response</code> object is an instance of <code>GrailsMockHttpServletResponse</code> (from the package <code>org.codehaus.groovy.grails.plugins.testing</code>) which extends Spring&#8217;s <code>MockHttpServletResponse</code> class and has a number of useful methods for inspecting the state of the response.</p>
</div>
<div class="paragraph">
<p>For example to test a redirect you can use the <code>redirectedUrl</code> property:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="type">class</span> <span class="class">SimpleController</span> {
    <span class="keyword">def</span> <span class="function">index</span>() {
        redirect <span class="key">action</span>: <span class="string"><span class="delimiter">'</span><span class="content">hello</span><span class="delimiter">'</span></span>
    }
    ...
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test index</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.index()

        <span class="key">then</span>:
        response.redirectedUrl == <span class="string"><span class="delimiter">'</span><span class="content">/simple/hello</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Many actions make use of the parameter data associated with the request. For example, the 'sort', 'max', and 'offset' parameters are quite common. Providing these in the test is as simple as adding appropriate values to a special <code>params</code> variable:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(PersonController)
<span class="type">class</span> <span class="class">PersonControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test list</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        params.sort = <span class="string"><span class="delimiter">'</span><span class="content">name</span><span class="delimiter">'</span></span>
        params.max = <span class="integer">20</span>
        params.offset = <span class="integer">0</span>
        controller.list()

        <span class="key">then</span>:
        <span class="comment">// ...</span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can even control what type of request the controller action sees by setting the <code>method</code> property of the mock request:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(PersonController)
<span class="type">class</span> <span class="class">PersonControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test save</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.method = <span class="string"><span class="delimiter">'</span><span class="content">POST</span><span class="delimiter">'</span></span>
        controller.save()

        <span class="key">then</span>:
        <span class="comment">// ...</span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This is particularly important if your actions do different things depending on the type of the request. Finally, you can mark a request as AJAX like so:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(PersonController)
<span class="type">class</span> <span class="class">PersonControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test list</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.method = <span class="string"><span class="delimiter">'</span><span class="content">POST</span><span class="delimiter">'</span></span>
        request.makeAjaxRequest()
        controller.getPage()

        <span class="key">then</span>:
        <span class="comment">// ...</span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>You only need to do this though if the code under test uses the <code>xhr</code> property on the request.</p>
</div>
</div>
<div class="sect3">
<h4 id="_testing_view_rendering">Testing View Rendering</h4>
<div class="paragraph">
<p>To test view rendering you can inspect the state of the controller&#8217;s <code>modelAndView</code> property (an instance of <code>org.springframework.web.servlet.ModelAndView</code>) or you can use the <code>view</code> and <code>model</code> properties provided by the mixin:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="type">class</span> <span class="class">SimpleController</span> {
    <span class="keyword">def</span> <span class="function">home</span>() {
        render <span class="key">view</span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">homePage</span><span class="delimiter">&quot;</span></span>, <span class="key">model</span>: [<span class="key">title</span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">Hello World</span><span class="delimiter">&quot;</span></span>]
    }
    ...
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test home</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.home()

        <span class="key">then</span>:
        view == <span class="string"><span class="delimiter">'</span><span class="content">/simple/homePage</span><span class="delimiter">'</span></span>
        model.title == <span class="string"><span class="delimiter">'</span><span class="content">Hello World</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that the view string is the absolute view path, so it starts with a '/' and will include path elements, such as the directory named after the action&#8217;s controller.</p>
</div>
</div>
<div class="sect3">
<h4 id="_testing_template_rendering">Testing Template Rendering</h4>
<div class="paragraph">
<p>Unlike view rendering, template rendering will actually attempt to write the template directly to the response rather than returning a <code>ModelAndView</code> hence it requires a different approach to testing.</p>
</div>
<div class="paragraph">
<p>Consider the following controller action:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="type">class</span> <span class="class">SimpleController</span> {
    <span class="keyword">def</span> <span class="function">display</span>() {
        render <span class="key">template</span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">snippet</span><span class="delimiter">&quot;</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>In this example the controller will look for a template in <code>grails-app/views/simple/_snippet.gsp</code>. You can test this as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test display</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.display()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">contents of the template</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>However, you may not want to render the real template, but just test that it was rendered. In this case you can provide mock Groovy Pages:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test display with mock template</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        views[<span class="string"><span class="delimiter">'</span><span class="content">/simple/_snippet.gsp</span><span class="delimiter">'</span></span>] = <span class="string"><span class="delimiter">'</span><span class="content">mock template contents</span><span class="delimiter">'</span></span>
        controller.display()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">mock template contents</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_testing_actions_which_return_a_map">Testing Actions Which Return A Map</h4>
<div class="paragraph">
<p>When a controller action returns a <code>java.util.Map</code> that <code>Map</code> may be inspected directly to assert that it contains the expected data:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="type">class</span> <span class="class">SimpleController</span> {
    <span class="keyword">def</span> <span class="function">showBookDetails</span>() {
        [<span class="key">title</span>: <span class="string"><span class="delimiter">'</span><span class="content">The Nature Of Necessity</span><span class="delimiter">'</span></span>, <span class="key">author</span>: <span class="string"><span class="delimiter">'</span><span class="content">Alvin Plantinga</span><span class="delimiter">'</span></span>]
    }
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test show book details</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        <span class="keyword">def</span> model = controller.showBookDetails()

        <span class="key">then</span>:
        model.author == <span class="string"><span class="delimiter">'</span><span class="content">Alvin Plantinga</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_testing_xml_and_json_responses">Testing XML and JSON Responses</h4>
<div class="paragraph">
<p>XML and JSON response are also written directly to the response. Grails' mocking capabilities provide some conveniences for testing XML and JSON response. For example consider the following action:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">def</span> <span class="function">renderXml</span>() {
    render(<span class="key">contentType</span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">text/xml</span><span class="delimiter">&quot;</span></span>) {
        book(<span class="key">title</span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">Great</span><span class="delimiter">&quot;</span></span>)
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This can be tested using the <code>xml</code> property of the response:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test render xml</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.renderXml()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">&quot;</span><span class="content">&lt;book title='Great'/&gt;</span><span class="delimiter">&quot;</span></span>
        response.xml.@title.text() == <span class="string"><span class="delimiter">'</span><span class="content">Great</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>xml</code> property is a parsed result from Groovy&#8217;s <a href="http://groovy-lang.org/processing-xml.html">XmlSlurper</a> class which is very convenient for parsing XML.</p>
</div>
<div class="paragraph">
<p>Testing JSON responses is pretty similar, instead you use the <code>json</code> property:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="comment">// controller action</span>
<span class="keyword">def</span> <span class="function">renderJson</span>() {
    render(<span class="key">contentType</span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">application/json</span><span class="delimiter">&quot;</span></span>) {
        book <span class="string"><span class="delimiter">&quot;</span><span class="content">Great</span><span class="delimiter">&quot;</span></span>
    }
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test render json</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.renderJson()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">{&quot;book&quot;:&quot;Great&quot;}</span><span class="delimiter">'</span></span>
        response.json.book == <span class="string"><span class="delimiter">'</span><span class="content">Great</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>json</code> property is an instance of <code>org.codehaus.groovy.grails.web.json.JSONElement</code> which is a map-like structure that is useful for parsing JSON responses.</p>
</div>
</div>
<div class="sect3">
<h4 id="_testing_xml_and_json_requests">Testing XML and JSON Requests</h4>
<div class="paragraph">
<p>Grails provides various convenient ways to automatically parse incoming XML and JSON packets. For example you can bind incoming JSON or XML requests using Grails' data binding:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">def</span> <span class="function">consumeBook</span>(<span class="predefined-type">Book</span> b) {
    render <span class="string"><span class="delimiter">&quot;</span><span class="content">The title is </span><span class="inline"><span class="inline-delimiter">${</span>b.title<span class="inline-delimiter">}</span></span><span class="content">.</span><span class="delimiter">&quot;</span></span>
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>To test this Grails provides an easy way to specify an XML or JSON packet via the <code>xml</code> or <code>json</code> properties. For example the above action can be tested by specifying a String containing the XML:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">grails.test.mixin.Mock</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="annotation">@Mock</span>([<span class="predefined-type">Book</span>])
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {
    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test consume book xml</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.xml = <span class="string"><span class="delimiter">'</span><span class="content">&lt;book&gt;&lt;title&gt;Wool&lt;/title&gt;&lt;/book&gt;</span><span class="delimiter">'</span></span>
        controller.consumeBook()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">The title is Wool.</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Or alternatively a domain instance can be specified and it will be auto-converted into the appropriate XML request:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">grails.test.mixin.Mock</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="annotation">@Mock</span>([<span class="predefined-type">Book</span>])
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test consume book xml</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.xml = <span class="keyword">new</span> <span class="predefined-type">Book</span>(<span class="key">title</span>: <span class="string"><span class="delimiter">'</span><span class="content">Shift</span><span class="delimiter">'</span></span>)
        controller.consumeBook()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">The title is Shift.</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The same can be done for JSON requests:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">grails.test.mixin.Mock</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="annotation">@Mock</span>([<span class="predefined-type">Book</span>])
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test consume book json</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.json = <span class="keyword">new</span> <span class="predefined-type">Book</span>(<span class="key">title</span>: <span class="string"><span class="delimiter">'</span><span class="content">Shift</span><span class="delimiter">'</span></span>)
        controller.consumeBook()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">The title is Shift.</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you prefer not to use Grails' data binding but instead manually parse the incoming XML or JSON that can be tested too. For example consider the controller action below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">def</span> <span class="function">consume</span>() {
    request.withFormat {
        xml {
            render <span class="string"><span class="delimiter">&quot;</span><span class="content">The XML Title Is </span><span class="inline"><span class="inline-delimiter">${</span>request.XML.@title<span class="inline-delimiter">}</span></span><span class="content">.</span><span class="delimiter">&quot;</span></span>
        }
        json {
            render <span class="string"><span class="delimiter">&quot;</span><span class="content">The JSON Title Is </span><span class="inline"><span class="inline-delimiter">${</span>request.JSON.title<span class="inline-delimiter">}</span></span><span class="content">.</span><span class="delimiter">&quot;</span></span>
        }
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>To test the XML request you can specify the XML as a string:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test consume xml</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.xml = <span class="string"><span class="delimiter">'</span><span class="content">&lt;book title=&quot;The Stand&quot;/&gt;</span><span class="delimiter">'</span></span>
        controller.consume()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">The XML Title Is The Stand.</span><span class="delimiter">'</span></span>
    }

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test consume json</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.json = <span class="string"><span class="delimiter">'</span><span class="content">{title:&quot;The Stand&quot;}</span><span class="delimiter">'</span></span>
        controller.consume()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">The JSON Title Is The Stand.</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_testing_mime_type_handling">Testing Mime Type Handling</h4>
<div class="paragraph">
<p>You can test mime type handling and the <code>withFormat</code> method quite simply by setting the request&#8217;s <code>contentType</code> attribute:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="comment">// controller action</span>
<span class="keyword">def</span> <span class="function">sayHello</span>() {
    <span class="keyword">def</span> data = [<span class="key">Hello</span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">World</span><span class="delimiter">&quot;</span></span>]
    request.withFormat {
        xml { render data <span class="keyword">as</span> grails.converters.XML }
        json { render data <span class="keyword">as</span> grails.converters.JSON }
        html data
    }
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test say hello xml</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.contentType = <span class="string"><span class="delimiter">'</span><span class="content">application/xml</span><span class="delimiter">'</span></span>
        controller.sayHello()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;map&gt;&lt;entry key=&quot;Hello&quot;&gt;World&lt;/entry&gt;&lt;/map&gt;</span><span class="delimiter">'</span></span>
    }

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test say hello json</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.contentType = <span class="string"><span class="delimiter">'</span><span class="content">application/json</span><span class="delimiter">'</span></span>
        controller.sayHello()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">{&quot;World&quot;}</span><span class="delimiter">'</span></span>[Hello]     }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>There are constants provided by <code>ControllerUnitTestMixin</code> for all of the common common content types as shown below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test say hello xml</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.contentType = XML_CONTENT_TYPE
        controller.sayHello()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;map&gt;&lt;entry key=&quot;Hello&quot;&gt;World&lt;/entry&gt;&lt;/map&gt;</span><span class="delimiter">'</span></span>
    }

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test say hello json</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        request.contentType = JSON_CONTENT_TYPE
        controller.sayHello()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">{&quot;World&quot;}</span><span class="delimiter">'</span></span>[Hello]     }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The defined constants are listed below:</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top"><strong>Constant</strong></th>
<th class="tableblock halign-left valign-top"><strong>Value</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">ALL_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">*/\*</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">FORM_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/x-www-form-urlencoded</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">MULTIPART_FORM_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">multipart/form-data</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">HTML_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">text/html</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">XHTML_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/xhtml+xml</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">XML_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/xml</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">JSON_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/json</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">TEXT_XML_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">text/xml</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">TEXT_JSON_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">text/json</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">HAL_JSON_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/hal+json</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">HAL_XML_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/hal+xml</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">ATOM_XML_CONTENT_TYPE</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">application/atom+xml</p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_testing_duplicate_form_submissions">Testing Duplicate Form Submissions</h4>
<div class="paragraph">
<p>Testing duplicate form submissions is a little bit more involved. For example if you have an action that handles a form such as:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">def</span> <span class="function">handleForm</span>() {
    withForm {
        render <span class="string"><span class="delimiter">&quot;</span><span class="content">Good</span><span class="delimiter">&quot;</span></span>
    }.invalidToken {
        render <span class="string"><span class="delimiter">&quot;</span><span class="content">Bad</span><span class="delimiter">&quot;</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>you want to verify the logic that is executed on a good form submission and the logic that is executed on a duplicate submission. Testing the bad submission is simple. Just invoke the controller:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test duplicate form submission</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.handleForm()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Bad</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Testing the successful submission requires providing an appropriate <code>SynchronizerToken</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="keyword">import</span> <span class="include">org.codehaus.groovy.grails.web.servlet.mvc.SynchronizerTokensHolder</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test valid form submission</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        <span class="keyword">def</span> tokenHolder = SynchronizerTokensHolder.store(session)

        params[SynchronizerTokensHolder.TOKEN_URI] = <span class="string"><span class="delimiter">'</span><span class="content">/controller/handleForm</span><span class="delimiter">'</span></span>
        params[SynchronizerTokensHolder.TOKEN_KEY] = tokenHolder.generateToken(params[SynchronizerTokensHolder.TOKEN_URI])
        controller.handleForm()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Good</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you test both the valid and the invalid request in the same test be sure to reset the response between executions of the controller:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="keyword">import</span> <span class="include">org.codehaus.groovy.grails.web.servlet.mvc.SynchronizerTokensHolder</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test form submission</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        controller.handleForm()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Bad</span><span class="delimiter">'</span></span>

        <span class="key">when</span>:
        response.reset()
        <span class="keyword">def</span> tokenHolder = SynchronizerTokensHolder.store(session)

        params[SynchronizerTokensHolder.TOKEN_URI] = <span class="string"><span class="delimiter">'</span><span class="content">/controller/handleForm</span><span class="delimiter">'</span></span>
        params[SynchronizerTokensHolder.TOKEN_KEY] = tokenHolder.generateToken(params[SynchronizerTokensHolder.TOKEN_URI])
        controller.handleForm()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Good</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_testing_file_upload">Testing File Upload</h4>
<div class="paragraph">
<p>You use the <code>GrailsMockMultipartFile</code> class to test file uploads. For example consider the following controller action:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">def</span> <span class="function">uploadFile</span>() {
    MultipartFile file = request.getFile(<span class="string"><span class="delimiter">&quot;</span><span class="content">myFile</span><span class="delimiter">&quot;</span></span>)
    file.transferTo(<span class="keyword">new</span> <span class="predefined-type">File</span>(<span class="string"><span class="delimiter">&quot;</span><span class="content">/local/disk/myFile</span><span class="delimiter">&quot;</span></span>))
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>To test this action you can register a <code>GrailsMockMultipartFile</code> with the request:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="keyword">import</span> <span class="include">org.codehaus.groovy.grails.plugins.testing.GrailsMockMultipartFile</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test file upload</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        <span class="keyword">def</span> file = <span class="keyword">new</span> GrailsMockMultipartFile(<span class="string"><span class="delimiter">'</span><span class="content">myFile</span><span class="delimiter">'</span></span>, <span class="string"><span class="delimiter">'</span><span class="content">some file contents</span><span class="delimiter">'</span></span>.bytes)
        request.addFile file
        controller.uploadFile()

        <span class="key">then</span>:
        file.targetFileLocation.path == <span class="string"><span class="delimiter">'</span><span class="content">/local/disk/myFile</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>GrailsMockMultipartFile</code> constructor arguments are the name and contents of the file. It has a mock implementation of the <code>transferTo</code> method that simply records the <code>targetFileLocation</code> and doesn&#8217;t write to disk.</p>
</div>
</div>
<div class="sect3">
<h4 id="_testing_command_objects">Testing Command Objects</h4>
<div class="paragraph">
<p>Special support exists for testing command object handling with the <code>mockCommandObject</code> method. For example consider the following action:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="type">class</span> <span class="class">SimpleController</span> {
    <span class="keyword">def</span> <span class="function">handleCommand</span>(SimpleCommand simple) {
        <span class="keyword">if</span>(simple.hasErrors()) {
            render <span class="string"><span class="delimiter">'</span><span class="content">Bad</span><span class="delimiter">'</span></span>
        } <span class="keyword">else</span> {
            render <span class="string"><span class="delimiter">'</span><span class="content">Good</span><span class="delimiter">'</span></span>
        }
    }
}

<span class="type">class</span> <span class="class">SimpleCommand</span> {
    <span class="predefined-type">String</span> name

    <span class="directive">static</span> constraints = {
        name <span class="key">blank</span>: <span class="predefined-constant">false</span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>To test this you mock the command object, populate it and then validate it as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test valid command object</span><span class="delimiter">'</span></span>() {
        <span class="key">given</span>:
        <span class="keyword">def</span> simpleCommand = <span class="keyword">new</span> SimpleCommand(<span class="key">name</span>: <span class="string"><span class="delimiter">'</span><span class="content">Hugh</span><span class="delimiter">'</span></span>)
        simpleCommand.validate()

        <span class="key">when</span>:
        controller.handleCommand(simpleCommand)

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Good</span><span class="delimiter">'</span></span>
    }

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test invalid command object</span><span class="delimiter">'</span></span>() {
        <span class="key">given</span>:
        <span class="keyword">def</span> simpleCommand = <span class="keyword">new</span> SimpleCommand(<span class="key">name</span>: <span class="string"><span class="delimiter">'</span><span class="delimiter">'</span></span>)
        simpleCommand.validate()

        <span class="key">when</span>:
        controller.handleCommand(simpleCommand)

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Bad</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The testing framework also supports allowing Grails to create the command object instance automatically. To test this invoke the no-arg version of the controller action method. Grails will create an instance of the command object, perform data binding on it using the request parameters and validate the object just like it does when the application is running. See the test below.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test valid command object</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        params.name = <span class="string"><span class="delimiter">'</span><span class="content">Hugh</span><span class="delimiter">'</span></span>
        controller.handleCommand()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Good</span><span class="delimiter">'</span></span>
    }

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test invalid command object</span><span class="delimiter">'</span></span>() {
        <span class="key">when</span>:
        params.name = <span class="string"><span class="delimiter">'</span><span class="delimiter">'</span></span>
        controller.handleCommand()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Bad</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_testing_allowedmethods">Testing allowedMethods</h4>
<div class="paragraph">
<p>The unit testing environment respects the <a href="../ref/Controllers/allowedMethods.html">allowedMethods</a> property in controllers.  If a controller action is limited to be accessed with certain request methods, the unit test must be constructed to deal with that.</p>
</div>
<div class="listingblock">
<div class="title">grails-app/controllers/com/demo/DemoController.groovy</div>
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">package</span> com.demo

<span class="type">class</span> <span class="class">DemoController</span> {

    <span class="directive">static</span> allowedMethods = [<span class="key">save</span>: <span class="string"><span class="delimiter">'</span><span class="content">POST</span><span class="delimiter">'</span></span>, <span class="key">update</span>: <span class="string"><span class="delimiter">'</span><span class="content">PUT</span><span class="delimiter">'</span></span>, <span class="key">delete</span>: <span class="string"><span class="delimiter">'</span><span class="content">DELETE</span><span class="delimiter">'</span></span>]

    <span class="keyword">def</span> <span class="function">save</span>() {
        render <span class="string"><span class="delimiter">'</span><span class="content">Save was successful!</span><span class="delimiter">'</span></span>
    }

    <span class="comment">// ...</span>
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">src/test/groovy/com/demo/DemoControllerSpec.groovy</div>
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">package</span> com.demo

<span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>
<span class="keyword">import</span> <span class="include">static</span> <span class="include">javax.servlet.http.HttpServletResponse.*</span>

<span class="annotation">@TestFor</span>(DemoController)
<span class="type">class</span> <span class="class">DemoControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">&quot;</span><span class="content">test a valid request method</span><span class="delimiter">&quot;</span></span>() {
        <span class="key">when</span>:
        request.method = <span class="string"><span class="delimiter">'</span><span class="content">POST</span><span class="delimiter">'</span></span>
        controller.save()

        <span class="key">then</span>:
        response.status == SC_OK
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Save was successful!</span><span class="delimiter">'</span></span>
    }

    <span class="type">void</span> <span class="string"><span class="delimiter">&quot;</span><span class="content">test an invalid request method</span><span class="delimiter">&quot;</span></span>() {
        <span class="key">when</span>:
        request.method = <span class="string"><span class="delimiter">'</span><span class="content">DELETE</span><span class="delimiter">'</span></span>
        controller.save()

        <span class="key">then</span>:
        response.status == SC_METHOD_NOT_ALLOWED
    }
}</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_testing_calling_tag_libraries">Testing Calling Tag Libraries</h4>
<div class="paragraph">
<p>You can test calling tag libraries using <code>ControllerUnitTestMixin</code>, although the mechanism for testing the tag called varies from tag to tag. For example to test a call to the <code>message</code> tag, add a message to the <code>messageSource</code>. Consider the following action:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">def</span> <span class="function">showMessage</span>() {
    render g.message(<span class="key">code</span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">foo.bar</span><span class="delimiter">&quot;</span></span>)
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This can be tested as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight"><code data-lang="groovy"><span class="keyword">import</span> <span class="include">grails.test.mixin.TestFor</span>
<span class="keyword">import</span> <span class="include">spock.lang.Specification</span>

<span class="annotation">@TestFor</span>(SimpleController)
<span class="type">class</span> <span class="class">SimpleControllerSpec</span> <span class="directive">extends</span> Specification {

    <span class="type">void</span> <span class="string"><span class="delimiter">'</span><span class="content">test render message tag</span><span class="delimiter">'</span></span>() {
        <span class="key">given</span>:
        messageSource.addMessage <span class="string"><span class="delimiter">'</span><span class="content">foo.bar</span><span class="delimiter">'</span></span>, request.locale, <span class="string"><span class="delimiter">'</span><span class="content">Hello World</span><span class="delimiter">'</span></span>

        <span class="key">when</span>:
        controller.showMessage()

        <span class="key">then</span>:
        response.text == <span class="string"><span class="delimiter">'</span><span class="content">Hello World</span><span class="delimiter">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>See <a href="#unitTestingTagLibraries">unit testing tag libraries</a> for more information.</p>
</div>
</div>


                <div style="clear:both;margin-top:15px;"></div>
                
                    <div class="toc-item prev-left"><a href="../../guide/staticTypeCheckingAndCompilation.html">&lt;&lt; <strong>14</strong><span>Static Type Checking And Compilation</span></a></div>
                
                    <div class="toc-item next-right"><a href="../../guide/i18n.html"><strong>16</strong><span>Internationalization</span> >></a></div>
                
                <div style="clear:both"></div>
            </div>
        </td>
        <td id="col2">
            <div class="local clearfix">
                <div class="local-title">
                    <a href="../../guide/index.html" target="mainFrame">Quick Reference</a>
                    <span class="toggle">(<a href="#" onclick="localToggle(); return false;">hide</a>)</span>
                </div>
                <div class="menu">
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Command Line</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Command%20Line/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/bug-report.html">bug-report</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/clean.html">clean</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/compile.html">compile</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/console.html">console</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-app.html">create-app</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-command.html">create-command</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-controller.html">create-controller</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-domain-class.html">create-domain-class</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-functional-test.html">create-functional-test</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-hibernate-cfg-xml.html">create-hibernate-cfg-xml</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-integration-test.html">create-integration-test</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-interceptor.html">create-interceptor</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-plugin.html">create-plugin</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-profile.html">create-profile</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-script.html">create-script</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-service.html">create-service</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-taglib.html">create-taglib</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/create-unit-test.html">create-unit-test</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/dependency-report.html">dependency-report</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/docs.html">docs</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/generate-all.html">generate-all</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/generate-controller.html">generate-controller</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/generate-views.html">generate-views</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/help.html">help</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/install-templates.html">install-templates</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/list-plugins.html">list-plugins</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/list-profiles.html">list-profiles</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/package-plugin.html">package-plugin</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/package.html">package</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/plugin-info.html">plugin-info</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/profile-info.html">profile-info</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/run-app.html">run-app</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/run-command.html">run-command</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/run-script.html">run-script</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/schema-export.html">schema-export</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/shell.html">shell</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/stats.html">stats</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/stop-app.html">stop-app</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/test-app.html">test-app</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Command%20Line/war.html">war</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Constraints</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Constraints/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Constraints/attributes.html">attributes</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/bindable.html">bindable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/blank.html">blank</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/creditCard.html">creditCard</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/email.html">email</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/inList.html">inList</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/matches.html">matches</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/max.html">max</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/maxSize.html">maxSize</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/min.html">min</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/minSize.html">minSize</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/notEqual.html">notEqual</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/nullable.html">nullable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/range.html">range</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/scale.html">scale</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/size.html">size</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/unique.html">unique</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/url.html">url</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/validator.html">validator</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Constraints/widget.html">widget</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Controllers</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Controllers/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Controllers/actionName.html">actionName</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/allowedMethods.html">allowedMethods</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/bindData.html">bindData</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/chain.html">chain</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/controllerName.html">controllerName</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/defaultAction.html">defaultAction</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/errors.html">errors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/flash.html">flash</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/forward.html">forward</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/grailsApplication.html">grailsApplication</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/hasErrors.html">hasErrors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/namespace.html">namespace</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/params.html">params</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/redirect.html">redirect</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/render.html">render</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/request.html">request</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/respond.html">respond</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/response.html">response</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/responseFormats.html">responseFormats</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/scope.html">scope</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/servletContext.html">servletContext</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/session.html">session</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/withForm.html">withForm</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Controllers/withFormat.html">withFormat</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Database Mapping</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/autoImport.html">autoImport</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/autoTimestamp.html">autoTimestamp</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/batchSize.html">batchSize</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/cache.html">cache</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/cascade.html">cascade</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/column.html">column</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/comment.html">comment</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/discriminator.html">discriminator</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/dynamicInsert.html">dynamicInsert</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/dynamicUpdate.html">dynamicUpdate</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/fetch.html">fetch</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/id.html">id</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/ignoreNotFound.html">ignoreNotFound</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/indexColumn.html">indexColumn</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/insertable.html">insertable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/joinTable.html">joinTable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/lazy.html">lazy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/order.html">order</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/sort.html">sort</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/table.html">table</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/type.html">type</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/updateable.html">updateable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Database%20Mapping/version.html">version</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Domain Classes</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/addTo.html">addTo</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/attach.html">attach</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/belongsTo.html">belongsTo</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/clearErrors.html">clearErrors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/constraints.html">constraints</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/count.html">count</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/countBy.html">countBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/createCriteria.html">createCriteria</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/delete.html">delete</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/discard.html">discard</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/embedded.html">embedded</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/errors.html">errors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/executeQuery.html">executeQuery</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/executeUpdate.html">executeUpdate</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/exists.html">exists</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/fetchMode.html">fetchMode</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/find.html">find</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findAll.html">findAll</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findAllBy.html">findAllBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findAllWhere.html">findAllWhere</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findBy.html">findBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findOrCreateBy.html">findOrCreateBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findOrCreateWhere.html">findOrCreateWhere</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findOrSaveBy.html">findOrSaveBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findOrSaveWhere.html">findOrSaveWhere</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/findWhere.html">findWhere</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/first.html">first</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/get.html">get</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/getAll.html">getAll</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/getDirtyPropertyNames.html">getDirtyPropertyNames</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/getPersistentValue.html">getPersistentValue</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/hasErrors.html">hasErrors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/hasMany.html">hasMany</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/hasOne.html">hasOne</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/ident.html">ident</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/instanceOf.html">instanceOf</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/isAttached.html">isAttached</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/isDirty.html">isDirty</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/last.html">last</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/list.html">list</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/listOrderBy.html">listOrderBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/load.html">load</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/lock.html">lock</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/mapWith.html">mapWith</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/mappedBy.html">mappedBy</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/mapping.html">mapping</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/merge.html">merge</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/namedQueries.html">namedQueries</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/properties.html">properties</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/read.html">read</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/refresh.html">refresh</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/removeFrom.html">removeFrom</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/save.html">save</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/transients.html">transients</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/validate.html">validate</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/where.html">where</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/whereAny.html">whereAny</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/withCriteria.html">withCriteria</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/withNewSession.html">withNewSession</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/withSession.html">withSession</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Domain%20Classes/withTransaction.html">withTransaction</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Plug-ins</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Plug-ins/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/URL%20mappings.html">URL mappings</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/codecs.html">codecs</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/controllers.html">controllers</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/core.html">core</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/dataSource.html">dataSource</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/domainClasses.html">domainClasses</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/hibernate.html">hibernate</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/i18n.html">i18n</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/logging.html">logging</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/scaffolding.html">scaffolding</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/services.html">services</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Plug-ins/servlets.html">servlets</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Services</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Services/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Services/scope.html">scope</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Services/transactional.html">transactional</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Servlet API</h1><div class="menu-sub">
                        
                            
                            <div class="menu-item"><a href="../../ref/Servlet%20API/request.html">request</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Servlet%20API/response.html">response</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Servlet%20API/servletContext.html">servletContext</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Servlet%20API/session.html">session</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Tag Libraries</h1><div class="menu-sub">
                        
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/Usage.html">Usage</a></div>
                            
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/actionName.html">actionName</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/controllerName.html">controllerName</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/flash.html">flash</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/pageScope.html">pageScope</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/params.html">params</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/request.html">request</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/response.html">response</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/servletContext.html">servletContext</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tag%20Libraries/session.html">session</a>
                            </div>
                            
                            </div>
                    </div>
                    
                    <div class="menu-block"><h1 class="menu-title" onclick="toggleRef(this.parentNode.childNodes[1])">Tags</h1><div class="menu-sub">
                        
                            
                            <div class="menu-item"><a href="../../ref/Tags/actionSubmit.html">actionSubmit</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/actionSubmitImage.html">actionSubmitImage</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/applyLayout.html">applyLayout</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/checkBox.html">checkBox</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/collect.html">collect</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/cookie.html">cookie</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/country.html">country</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/countrySelect.html">countrySelect</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/createLink.html">createLink</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/createLinkTo.html">createLinkTo</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/currencySelect.html">currencySelect</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/datePicker.html">datePicker</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/each.html">each</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/eachError.html">eachError</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/else.html">else</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/elseif.html">elseif</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/external.html">external</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/field.html">field</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/fieldError.html">fieldError</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/fieldValue.html">fieldValue</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/findAll.html">findAll</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/form.html">form</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/formatBoolean.html">formatBoolean</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/formatDate.html">formatDate</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/formatNumber.html">formatNumber</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/grep.html">grep</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/hasErrors.html">hasErrors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/header.html">header</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/hiddenField.html">hiddenField</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/if.html">if</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/img.html">img</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/include.html">include</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/isAvailable.html">isAvailable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/isNotAvailable.html">isNotAvailable</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/javascript.html">javascript</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/join.html">join</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/layoutBody.html">layoutBody</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/layoutHead.html">layoutHead</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/layoutTitle.html">layoutTitle</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/link.html">link</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/localeSelect.html">localeSelect</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/message.html">message</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/meta.html">meta</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/pageProperty.html">pageProperty</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/paginate.html">paginate</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/passwordField.html">passwordField</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/radio.html">radio</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/radioGroup.html">radioGroup</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/render.html">render</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/renderErrors.html">renderErrors</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/resource.html">resource</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/select.html">select</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/set.html">set</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/sortableColumn.html">sortableColumn</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/submitButton.html">submitButton</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/textArea.html">textArea</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/textField.html">textField</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/timeZoneSelect.html">timeZoneSelect</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/unless.html">unless</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/uploadForm.html">uploadForm</a>
                            </div>
                            
                            <div class="menu-item"><a href="../../ref/Tags/while.html">while</a>
                            </div>
                            
                            </div>
                    </div>
                    
                </div>
            </div>
        </td>
    </tr>
</table>

<div id="footer">
    Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
    
</div>

<script type="text/javascript" src="../js/docs.js"></script>

</body>
</html>
